﻿using System.Collections.Generic;
using System.Windows;
using Renren.Helpers;
using Renren.Models;

namespace Renren.ViewModels
{
    public class NewsViewModel : ViewModel<NewsModel>, IHeaderViewModel
    {
        public NewsViewModel() : base() { }
        public NewsViewModel(NewsModel news):base(news){}

        private string GetPhotoUrl(int idx)
        {   
            return Model.MediaList != null && Model.MediaList.Count > idx ?
                   Model.MediaList[idx].Src :
                   null;
        }

        public string Title
        {
            get { return Model.Title; }
        }

        public string Content
        {
            get
            {
                string content = null;
                switch (Model.FeedType)
                {
                    case FeedType.UpdateStatus:
                        foreach (var media in Model.MediaList)
                        {
                            content = media.Content;
                        }
                        break;
                    default:
                        content = Model.Description;
                        break;
                }
                return content;
            }
        }

        public string NewsType
        {
            get
            {
                return Model.FeedType.GetString();
            }
        }

        public string UpdateTime
        {
            get { return Model.UpdateTime.GetFriendlyTime(); }
        }

        public string CommentCount
        {
            get
            {
                // Set comment count
                int count = int.Parse(Model.CommentCount);
                if (count > 99)
                {
                    return "99+";
                }
                else
                {
                    return count.ToString();
                }
            }
        }

        public Visibility ContentPanel
        {
            get
            {
                if (this.Content != null)
                {
                    return Visibility.Visible;
                }
                else
                {
                    return Visibility.Collapsed;
                }
            }
        }
        
        public Visibility PhotoStackPanel
        {
            get
            {
                if ((Model.FeedType == FeedType.UploadPhoto ||
                     Model.FeedType == FeedType.SharePhoto) &&
                     Model.MediaList.Count > 0)
                {
                    return Visibility.Visible;
                }
                else
                {
                    return Visibility.Collapsed;
                }
            }
        }

        public string Photo0_Url
        {
            get { return GetPhotoUrl(0); }
        }

        public string Photo1_Url
        {
            get { return GetPhotoUrl(1); }
        }

        public string Photo2_Url
        {
            get { return GetPhotoUrl(2); }
        }

        public string Photo3_Url
        {
            get { return GetPhotoUrl(3); }
        }

        public string HeadUrl
        {
            get { return Model.HeadUrl; }
        }

        public string Name
        {
            get { return Model.Name; }
        }

        public long UserId
        {
            get { return Model.ActorId; }
        }
    
        protected override List<string> PropertyNames
        {
            get
            {
                return new List<string>
                {
                    "CommentCount",
                    "Content",
                    "ContentPanel",
                    "HeadUrl",
                    "Name",
                    "NewsType",
                    "Photo0_Url",
                    "Photo1_Url",
                    "Photo2_Url",
                    "Photo3_Url",
                    "PhotoStackPanel",
                    "Title",
                    "UpdateTime"
                };
            }
        }
    }
}
